using MultiAgentsShared;

namespace MultiAgentsClient.Service.WorkflowEngine.BaseNodes;

public class NodeToolCallRequest : NodeRequestToService
{
    public string SelectedTool;
    public List<ToolCallParams> ToolCallParams;

    public NodeToolCallRequest(string nodeId) : base(nodeId) { }
}

public class NodeToolCallResponse : NodeResponseFromService
{
    public string response;
    public NodeToolCallResponse(string nodeId) : base(nodeId) { }
}

public class ToolCallParams
{
    public string type { get; set; }
    public string name { get; set; }
    public string value { get; set; }

    public ToolCallParams(string type, string name, string value)
    {
        this.type = type;
        this.name = name;
        this.value = value;
    }
}

public abstract class ToolCallNodeBase : ServiceDependentNode<NodeToolCallRequest, NodeToolCallResponse>
{
    protected virtual string SelectedTool { get; set; }

    protected List<ToolCallParams> ToolCallParams = [];
    private List<ToolCallParams> ToolCallParamsRuntime => InsertVariable(ToolCallParams);

    [NodeGraphOutput] protected string ToolCallResponse;

    public ToolCallNodeBase() : base()
    {
    }

    protected override NodeOutcome ServiceResponseHandler(NodeToolCallResponse cmd)
    {
        Debug.LogLine("ToolCallResponse:" + cmd.response);
        ToolCallResponse = cmd.response;
        runtimeContext.SetVar(Id, () => ToolCallResponse);
        return new NodeOutcome(NodeOutcomeStatus.Success, 0);
    }

    protected override void SendRequestToService()
    {
        var toolCallRequest = new NodeToolCallRequest(Id)
        {
            SelectedTool = SelectedTool,
            ToolCallParams = ToolCallParamsRuntime
        };
        EventBus.Publish(toolCallRequest);
    }
}
